/**
 * Copyright (C) 2009 Noel Ang
 *
 * This file is part of Samsara.
 *
 * Samsara is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Samsara is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Samsara.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package com.lunarshades.samsara.ui.actions;

import com.lunarshades.samsara.Raider;
import com.lunarshades.samsara.ui.Utils;

/**
 * A KarmaAddEvent represents a positive karma change.
 *
 * @author Noel Ang, sparta.variable@gmail.com
 */
public class KarmaAddEvent<T extends Raider> extends KarmaChangeEvent<T> {

/**
 * Create a KarmaAddEvent.
 *
 * @param subject The subject of the change.
 * @param amount  The magnitude of the change. Must be a zero or non-negative
 *                value.
 * @param context Additional info describing the context under which the event
 *                is taking place. (Optional)
 *
 * @throws NullPointerException     if subject is null.
 * @throws IllegalArgumentException if amount is a negative value.
 */
public KarmaAddEvent(T subject, int amount, String context) {
    super(subject, amount);
    mContext = clean(context);
}

protected String initMessage() {
    // GAINED  [Person] karma [2] (10 +> 12) for [Epic Loot of Shit]
    StringBuffer buffer = new StringBuffer();

    buffer.append("GAINED ");

    // person
    buffer.append("[").append(subject().name()).append("] ");

    // karma amount
    buffer.append("karma [");
    buffer.append(magnitude()).append("] ");

    // total karma
    buffer.append("(");
    try {
        int newKarma = Utils.makeInt(subject().karma().toString());
        int oldKarma = newKarma - magnitude();
        buffer.append(oldKarma)
                .append(" +> ")
                .append(newKarma);
    } catch (NumberFormatException e) {
        buffer.append("ERR: ").append(subject().karma().toString());
    } finally {
        buffer.append(")");
    }

    // context
    if (!"".equals(mContext)) {
        buffer.append(" for [").append(mContext).append("]");
    }
    return buffer.toString();
}

private String clean(String value) {
    if (value == null) {
        value = "";
    } else {
        value = value.trim();
    }
    return value;
}

public String context() {
    return mContext;
}

/**
 * @return A representative message.
 */
public Object message() {
    if (mMessage == null) {
        mMessage = initMessage();
    }
    return mMessage;
}

private volatile String mMessage;
private final String mContext;
}
